<template>
	<view class="addCard">
		<!-- <navCom navName="银行卡" @previous="previous"></navCom> -->
		<view class="add_box my-pdlr-15 my-mgt-15">
			<!-- <view class="add_box_li_tlt">
				<view class="add_box_name my-mgr-10">姓名</view>
				<input type="text" class="add_box_phone" v-model.lazy="name" placeholder-class="inp_pla" placeholder="请输入姓名" />
			</view>
			<view class="add_box_li_tlt">
				<view class="add_box_name my-mgr-10">身份证号</view>
				<input type="text" class="add_box_phone" v-model.lazy="idCar" placeholder-class="inp_pla" placeholder="请输入身份证号" />
			</view> -->
			<view class="add_box_li_tlt">
				<view class="add_box_name my-mgr-10">账户号</view>
				<input type="text" class="add_box_phone" v-model.lazy="bankCard" placeholder-class="inp_pla" placeholder="请填写您的账户号" />
			</view>
			<view class="add_box_li_tlt" @click="choseCard">
				<view class="add_box_name my-mgr-10">账户渠道</view>
				<view class="add_box_phone my-mgr-5" style="color:#CCCCCC;" v-if="bankName===''">请选择</view>
				<view class="add_box_phone my-mgr-5" style="color:#545454;" v-else>{{bankName}}</view>
				<image src="https://hxc-pay.oss-cn-shenzhen.aliyuncs.com/wQnZZqNAjB_rightarrow_icon.png" mode="" style="width: 16rpx;height: 16rpx;"></image>
			</view>
			<view class="add_box_li_tlt">
				<view class="add_box_name my-mgr-10">手机号码</view>
				<input type="text" class="add_box_phone" v-model.lazy="phone" placeholder-class="inp_pla" placeholder="请填写手机号码" />
			</view>
			<view class="add_box_li_tlt">
				<view class="add_box_name my-mgr-10">短信验证码</view>
				<input type="text" class="inp" v-model.lazy="verificationCode" placeholder-class="inp_pla"
					placeholder="请填写" />
				<view class="contant_box_li_txt my-mgl-10">
					<view v-if="anew">{{countDownNum}}s后重新获取</view>
					<view v-else @click="getCode">获取验证码</view>
				</view>
			</view>
			<view class="complete">
				<button class="btn" @click="conmincard">提交</button>
			</view>
			<view class="radio-btn my-flex-col-c">
				<view class="my-flex-row-align-c">
					<radio class="tr" color="#F05329" @click="changeRadio" style="transform: scale(0.7);"  :checked="checked"/>
					<text class="my-cl-ye my-mgl-8 my-font-15" @click="goAgreement">我已阅读并同意<text class="my-cl-or">《用户协议》</text></text>
				</view>
			</view>
		</view>
		<u-popup v-model="QSPopup" mode="center">
			<view class="codebox">
				<view class="tlt my-mgb-10">请输入图形验证码</view>
				<view class="image-code">
					<input type="text" v-model="verifyCode" class="input" placeholder="输入图片中的图形" />
					<view class="img" @click="getVCode">
						<image :src="imageCode" mode="" class="code-img"></image>
						<view class="tishi">看不清？换一张</view>
					</view>
				</view>
				<view class="btnbox">
					<view class="left" @click="cancel">取消</view>
					<view class="right" @click="sendCode">确定</view>
				</view>
			</view>
		</u-popup>
		<u-popup v-model="selectBack" mode="bottom" border-radius="40">
			<view class="screen_box">
				<view class="screen_box_tlt my-mgtb-15">
					<view class="tlt">选择账户渠道</view>
					<image src="https://hxc-pay.oss-cn-shenzhen.aliyuncs.com/aYUwDbroiD_open_icon.png" mode=""
						@click="closeup" style="width: 28rpx;height: 28rpx;"></image>
				</view>
				<scroll-view scroll-view scroll-y scroll-with-animation style="height: 620rpx;width: 100%;">
					<view class="list_box my-pdlr-15">
						<view class="screen_box_ul" v-for="(item,index) in cardArr" :key="index"
							@click="chosecard(item.id,item)">
							<image :src="item.bankLogo" mode="" class="img_fir my-mgr-15"></image>
							<view class="li_box">
								{{item.bankName}}
							</view>
							<image
								:src="cardids===item.id?'https://hxc-pay.oss-cn-shenzhen.aliyuncs.com/gGkGiWKeSr_check_icon.png':''"
								mode="" class="img_check"></image>
						</view>
					</view>
				</scroll-view>
				<view class="screenBtn_box">
					<button class="screenBtn" @click="okCard">确定</button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import navCom from "@/components/navCom/navCom.vue";
	import { getVerifyCodeApi , sendMsgApi } from "@/api/uesr.js"
	import { addcardApi ,getbankinfoApi } from "@/api/home.js"
	export default {
		components: {
			navCom,
		},
		data() {
			return {
				verificationCode: '', //短信验证码
				countDownNum: 60, // 倒计时初始值
				anew: false, //重新获取
				imageCode: '', //图形验证码
				QSPopup: false, //图形验证控制
				verifyCode: '',
				bankCard:'',//账户hao
				name:'',//姓名
				idCar:null,//身份证
				bankCode:'',//账户编码
				bankName:'',
				phone:null,
				bankLogo:'',
				selectBack:false,
				cardArr:[],
				cardids: 0, //选择账户渠道id
				checked:false,
			};
		},
		onLoad() {
			this.getBankInfo()
		},
		methods: {
			// radio切换
			changeRadio(){
				this.checked = !this.checked
			},
			goAgreement(){
				wx.navigateTo({
					url:'/pages/my/agreement'
				})
			},
			// 关闭选择账户
			closeup() {
				this.selectBack = false
			},
			// 打开选择账户
			choseCard() {
				this.selectBack = true
			},
			// 选择账户号
			chosecard(id,item) {
				this.cardids = id
				this.bankCode = item.bankCode
				this.bankName = item.bankName
				this.bankLogo = item.bankLogo
			},
			// 确定账户
			okCard() {
				this.selectBack = false
			},
			// 提交账户
			async conmincard(){
				// if(this.name===''){
				// 	uni.showToast({
				// 		title:"请输入名字",
				// 		icon:'none'
				// 	})
				// 	return 
				// } 
				// if(this.idCar === ''){
				// 	uni.showToast({
				// 		title:"请输入身份证号",
				// 		icon:'none'
				// 	})
				// 	return 
				// }
				if(this.bankCard === ''&&this.whereis === ''){
					uni.showToast({
						title:"请输入账户号",
						icon:'none'
					})
					return 
				}
				if(this.phone === ''){
					uni.showToast({
						title:"请输入手机号",
						icon:'none'
					})
					return 
				}
				if(this.verificationCode === ''){
					uni.showToast({
						title:"请输入短信验证码",
						icon:'none'
					})
					return 
				}
				if(this.checked===false){
					uni.showToast({
						title:"请勾选用户协议",
						icon:"none"
					})
					return
				}
				let params ={
					bankCardNumber:this.bankCard,
					bankCode:this.bankCode,
					bankLogo:this.bankLogo,
					bankName:this.bankName,
					// idCard:this.idCar,
					phone:this.phone,
					// realName:this.name,
					smsCode:this.verificationCode
				}
				uni.showLoading({
					title:"请稍等...."
				})
				const res = await addcardApi(params)
				if(res.statusCode === 200){
					uni.hideLoading()
					uni.showToast({
						title: '绑定成功！',
						icon: 'success',
						duration:5000,
						success() {
							uni.redirectTo({
								url:"/pages/my/bankCard"
							})
						}
					})
				}
			},
			async getBankInfo(){
				uni.showLoading({
					title:"请稍等...."
				})
				const res = await getbankinfoApi({})
				if(res.statusCode === 200){
					uni.hideLoading()
					this.cardArr = [...this.cardArr,...res.data.data]
				}
			},
			// 验证码倒计时
			getCode() {
				if (this.phone) {
					var regex = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
					var bool = regex.test(this.phone)
					if (bool) {
						this.QSPopup = true
						this.getVCode()
					} else {
						uni.showToast({
							title: "请输入正确的手机号",
							icon: "none"
						})
					}
				} else {
					uni.showToast({
						title: "请输入手机号",
						icon: "none"
					})
				}
			},
			// 获取图形验证码
			async getVCode() {
				uni.showLoading({
					title: "正在加载中...."
				})
				const res = await getVerifyCodeApi({})
				if (res.statusCode === 200) {
					uni.hideLoading()
					let imageBase64 = 'data:image/png;base64,' + wx.arrayBufferToBase64(res.data)
					this.imageCode = imageBase64
				} else {
					uni.showToast({
						title: '出错了，请重新获取',
						icon: "none"
					})
				}
			},
			countDown() {
				var _this = this;
				_this.anew = true
				var countDownNum = _this.countDownNum // 获取倒计时初始值
				var timer = setInterval( ()=> {
					countDownNum -= 1
					_this.countDownNum = countDownNum
					if (countDownNum <= -1) {
						clearInterval(timer)
						// 取消置顶的setInterval函数将要执行的代码
						_this.countDownNum = 60
						_this.anew = false
						
					}
				}, 1000)
			},
			// 获取图形验证码
			async getVCode() {
				uni.showLoading({
					title: "正在加载中...."
				})
				const res = await getVerifyCodeApi({})
				if (res.statusCode === 200) {
					uni.hideLoading()
					let imageBase64 = 'data:image/png;base64,' + wx.arrayBufferToBase64(res.data)
					this.imageCode = imageBase64
				} else {
					uni.showToast({
						title: '出错了，请重新获取',
						icon: "none"
					})
				}
			},
			//取消
			cancel() {
				this.QSPopup = false;
			},
			// 发送验证码
			async sendCode(ref) {
				if (this.verifyCode === "") {
					uni.showToast({
						title: "请先输入图形验证码！",
						icon: "none"
					})
					return
				} else {
					let params = {
						phone: this.phone,
						verifyCode: this.verifyCode
					}
					uni.showLoading({
						title: '正在加载中....'
					})
					const res = await sendMsgApi(params)
					console.log(res, "res");
					if (res.statusCode == 200) {
						uni.hideLoading()
						this.QSPopup = false;
						this.anew = true
						this.countDown()
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						})
					}
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	page{
		background:#FFFFFF;
	}
	.addCard{
		width: 100%;
		height: 100vh;
		background: #FAFAFA;
		.add_box{
			width: 100%;
			height: 1152rpx;
			background: #FFFFFF;
			.add_box_li_tlt{
				width: 100%;
				height: 110rpx;
				border-bottom: 1rpx solid #F5F5F5;
				display: flex;
				align-items: center;
				justify-content: space-between;
				.add_box_name{
					width: 30%;
					font-size: 28rpx;
					font-weight: 500;
					color: #000000;
				}
				.add_box_phone{
					width: 65%;
					font-size: 28rpx;
					font-weight: 500;
					text-align: right;
				}
				.inp_pla{
					text-align: right;
					font-size: 28rpx;
					font-weight: 500;
					color: #CCCCCC;
				}
				.inp {
					width: 53%;
					line-height: 100rpx;
				}
				.contant_box_li_txt {
					width: 40%;
					height: 30rpx;
					line-height: 30rpx;
					text-align: right;
					font-size: 28rpx;
					font-weight: 500;
					color: #F05329;
					border-left: 4rpx solid #CCCCCC;
				}
			}
			.complete{
				width: 100%;
				height: 200rpx;
				display: flex;
				align-items: flex-end;
				.btn{
					width: 690rpx;
					line-height: 88rpx;
					margin: 0 auto;
					background: #F05329;
					border-radius: 10rpx;
					font-size: 38rpx;
					font-weight: 500;
					color: #FFFFFF;
				}
			}
		}
		.codebox {
			width: 690rpx;
			height: 376rpx;
			margin: auto;
			background: #FFFFFF;
			border-radius: 20rpx;
		
			.tlt {
				width: 100%;
				height: 110rpx;
				text-align: center;
				line-height: 110rpx;
				font-size: 32rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #333333;
			}
		
			.image-code {
				width: 100%;
				height: 130rpx;
				display: flex;
				padding-left: 30rpx;
		
				.input {
					font-size: 32rpx;
					color: #000;
					width: 350rpx;
					height: 70rpx;
					border: 1rpx solid #999999;
					border-radius: 10rpx;
					margin-right: 30rpx;
					padding-left: 20rpx;
				}
		
				.img {
					width: 270rpx;
					height: 100%;
		
					.code-img {
						width: 90%;
						height: 80upx;
					}
		
					.tishi {
						width: 90%;
						height: 40rpx;
						text-align: center;
						line-height: 40rpx;
						font-size: 24rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #999999;
					}
				}
			}
		
			.btnbox {
				width: 100%;
				height: 95rpx;
				margin-top: 23rpx;
				display: flex;
				border-top: 1rpx solid #999999;
		
				.right,
				.left {
					width: 50%;
					height: 100%;
					text-align: center;
					line-height: 95rpx;
					font-size: 32rpx;
					font-family: PingFang SC;
					font-weight: 500;
				}
		
				.right {
					color: #F05329;
					border-left: 1rpx solid #999999;
				}
		
				.left {
					color: #999999;
				}
			}
		}
		.screen_box {
			width: 750rpx;
			height: auto;
			min-height: 850rpx;
			background: #FFFFFF;
		
			.screen_box_tlt {
				width: 100%;
				height: 70rpx;
				display: flex;
				align-items: center;
		
				.tlt {
					width: 90%;
					height: 100%;
					text-align: center;
					line-height: 80rpx;
					font-size: 34rpx;
					font-weight: 500;
					color: #000000;
					padding-left: 70rpx;
		
				}
			}
		
			.list_box {
				width: 100%;
				height: 620rpx;
			}
		
			.screen_box_ul {
				width: 100%;
				height: 120rpx;
				border-bottom: 1rpx solid #E8E8E8;
				display: flex;
				align-items: center;
		
				.li_box {
					width: 85%;
					height: 100%;
					line-height: 120rpx;
					font-size: 30rpx;
					font-weight: 400;
					color: #515151;
				}
		
				.img_fir {
					width: 60rpx;
					height: 60rpx;
				}
		
				.img_check {
					width: 30rpx;
					height: 28rpx;
				}
			}
		
			.screenBtn_box {
				width: 100%;
				height: 88rpx;
		
				.screenBtn {
					width: 690rpx;
					height: 88rpx;
					margin: auto;
					background: #F05329;
					border-radius: 10rpx;
					font-size: 38rpx;
					font-weight: 500;
					color: #FFFFFF;
					text-align: center;
					line-height: 88rpx;
				}
			}
		}
		.radio-btn{
			width: 100%;
			height: 80rpx;
			position: absolute;
			left: 15rpx;
			bottom: 40rpx;
		}
	}

</style>
